package com.unitd.modules.comm.conf;

import com.unitd.modules.sso.interceptor.SecurityInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @desc 自定义的SpringMVC的WebMVC的配置适配器类
 * @filename MyWebMvcConfigurerAdapter.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2017/2/5
 */
@Configuration
public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {

	/**
	 * @desc 将指定的拦截器注册到容器中
	 * @param registry 拦截器注册容器对象
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/**").excludePathPatterns("/login.html", "/auth/**");
		super.addInterceptors(registry);
	}

	/**
	 * @desc 将指定的资源文件向资源文件处理注册器注册
	 * @param registry 资源文件注册器
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
		registry.addResourceHandler("/**").addResourceLocations("classpath:/templates/");
		super.addResourceHandlers(registry);
	}

	/**
	 * @desc 首页请求强制跳转
	 * @param registry 视图控制器注册容器
	 */
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("").setViewName("/index.html");
		registry.addViewController("/").setViewName("/index.html");
		registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
		super.addViewControllers(registry);
	}
}